AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
AddCSLuaFile( "specialchars.lua" )
AddCSLuaFile( "sh_player.lua" )

include( 'sh_player.lua' )
include( 'shared.lua' )
include( 'specialchars.lua' )

function GM:PlayerSpawn( ply )  //What happens when the player spawns
 
    self.BaseClass:PlayerSpawn( ply )   
 
    ply:SetGravity( 0.75 )  
    ply:SetMaxHealth( 100, true )  
 
    ply:SetWalkSpeed( 325 )  
	ply:SetRunSpeed( 325 ) 
 
end
 
 function GM:PlayerInitialSpawn( ply )  // When spawning after joining the sever
	CheckSpecialCharacters( ply ) //Is he defined in the specialchars.lua file? If yes, then he gets those values, and it stops here.
	if ply:IsAdmin() then //Is the connecting player admin?
		sb_team2( ply ) //If he is then set his team to team 2.
	else // If he isn't admin then,
	joining( ply ) //Call the function joining (found near the bottom of this file)
	RunConsoleCommand( "sb_start" )	 //Run the console command defined in cl_init.lua. 
	end //Close the if
end //close the function

function GM:PlayerLoadout( ply ) // What should the player recieve when joining a team?
 
	if ply:Team() == 1 then //If he is team 1, then give him the following items
 
		ply:Give( "weapon_physcannon" ) // A Gravity gun
		ply:Give( "weapon_physgun" ) // A Physics gun
		ply:Give( "gmod_tool" ) // and don't forget the tool gun!
		ply:Give( "gmod_camera" )
 
 
	elseif ply:Team() == 2 then // So if he isn't team 1, he could be team 2?
 
		ply:Give( "weapon_physcannon" ) //Assuming he is, then give him Gravity gun
		ply:Give( "weapon_physgun" ) // Physics gun
		ply:Give( "weapon_ar2" ) // AR2 
		ply:Give( "gmod_tool" ) // and the gmod tool
 
	//I should mention at this point you can put in else, but there is no point. All possible scenarios are covered. Thus we end it	
	end //right here.
end // End the function

 
function sb_team1( ply ) //This is what happens when we enter sb_team1 into the console.
 
	ply:UnSpectate() //Since he was set to spectate until he presses the 'hell yeah' button, we now unspecatate him
	ply:SetTeam( 1 ) //We set his team to one, a.k.a 'guest'
	ply:Spawn() //Spawn the player
	ply:PrintMessage( HUD_PRINTTALK, "[KoreBuild]Welcome to the server, " .. ply:Nick() ) //Gives the message [SimpleBuild]Welcome to the server, (playername here)"  in the talk area.
 
end //End the function.

function sb_team2( ply ) // Function sb_team2. Called at the beginning
	// Why no unspectate? Look carefully at the GM:PlayerSpawn; We only call to spectate after we see if he's an admin. Assuming he's always ready to build, I chose to skip it.	
	ply:SetTeam( 2 ) //Set his team to team 2.
	ply:Spawn() // Spawn him
	ply:PrintMessage( HUD_PRINTTALK, "[KoreBuild]Your admin, so I'm going to give you magical powers " .. ply:Nick() ) //Again, a message in the talk area. 
	//This time saying "[KoreBuild] I recognize you as an admin (playername here)" 
 
end //End this function

concommand.Add( "sb_team1", sb_team1 ) //Now, we make sure that when we enter sb_team1 into console that it calls the function. This is KEY. Otherwise players won't be able to play.
 
function joining( ply ) // The function that's called when the player is not admin or a special character, at the top.
 
	ply:Spectate( 5 ) //Set him to spectate in free-roam mode. He doesn't actually fly around, since he has a window open at this point.
	ply:SetTeam( 4 ) //Set his team to Joining
 
end //End the function

 
MONEY_STARTAMOUNT = 1000 --Can be changed to your starting amount
 
function FirstSpawn( ply )
	local cash = ply:GetPData("money") --Get the saved money amount
 
	if cash == nil then --If it doesn't exist supply the player with the starting money amount
		ply:SetPData("money", MONEY_STARTAMOUNT) --Save it
		ply:SetMoney( MONEY_STARTAMOUNT ) --Set it to the networked ints that can be called from the client too
	else
	ply:SetMoney( cash ) --If not, set the networked ints to what we last saved
	end
 
end
 
function PrintCash( pl )
	pl:ChatPrint("Your cash is: " .. pl:GetMoney())
end
 
function fPlayerDisconnect( ply )
	print("Player Disconnect: Money saved to SQLLite and TXT")
	ply:SaveMoney()
	ply:SaveMoneyTXT()
end
 
concommand.Add("cash_get",PrintCash)
 
hook.Add( "PlayerInitialSpawn", "playerInitialSpawn", FirstSpawn )
 
 
 